package com.plsjavaweb.ui.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.plsjavaweb.core.EmployeeSeatCore;
import com.plsjavaweb.logging.LoggingConstants;
import com.plsjavaweb.pojo.EmployeeSeatPOJO;
import com.plsjavaweb.ui.controler.modelview.ErrorPage;

/**
 * Servlet implementation class SeatMap
 */
@WebServlet("/SeatMap")
public class SeatMap extends HttpServlet {
	static Logger log = Logger.getLogger(Login.class);
	static LoggingConstants logConst = new LoggingConstants();
	
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SeatMap() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		log.info(logConst.entryMessage);
		
		EmployeeSeatCore empSeatCore = new EmployeeSeatCore();
		HttpSession currentSession = request.getSession(true);
		
		if(currentSession.getAttribute("user") == null){
			log.info(LoggingConstants.sessionExpired);
			ErrorPage errorPage = new ErrorPage();
			errorPage.setErrorTitle("Error Page");
			errorPage.setErrorHeader("Invalid Session");
			errorPage.setErrorContent("Session has expired. Please Log-in to start new session");
			errorPage.setReturnUrl("/pls-java-web/Login");
			errorPage.setReturnUrlTitle("Go Back to Login Page");
			request.setAttribute("error", errorPage);
			RequestDispatcher rd = request.getRequestDispatcher("error-page.jsp");
			rd.forward(request, response);
		}
		else
		{
			log.info("forward to seat map page");
			List<EmployeeSeatPOJO> empSeats = empSeatCore.getEmployeeSeatsByBldgByFloor("PIC", "3F");
			request.setAttribute("employeeSeats", empSeats);
			request.getRequestDispatcher("WEB-INF/seat-map.jsp").forward(request, response);
		}
		
		log.info(logConst.exitMessage);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
